package cn.vendsystem.com.controller;

import cn.vendcommon.com.annotation.RequestLogin;
import cn.vendcommon.com.entity.constants.CommonConstants;
import cn.vendcommon.com.entity.Result;
import cn.vendcommon.com.entity.User;
import cn.vendcommon.com.entity.vo.TokenVO;
import cn.vendsystem.com.AOP.AdminCheck;
import cn.vendsystem.com.service.UserService;
import jakarta.validation.constraints.Pattern;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserService userService;


    /**
     * 用户注册
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("/register")
    @RequestLogin
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String phone , @Pattern(regexp = "^\\S{5,16}$") String password){
        User user = userService.findByPhone(phone);
        if(user == null){
            userService.register(phone,password);
            return Result.success();
        }else{
            return Result.error("用户名已被占用");
        }
    }

    /**
     * 用户登录
     * @param token
     * @param ip
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("/loginAdmin")
    @RequestLogin
    @AdminCheck("phone")
    public Result<TokenVO> login_Admin(
            @RequestHeader(value = CommonConstants.TOKEN_NAME, required = false) String token,
            @RequestHeader(value = CommonConstants.REAL_IP, required = false) String ip
            ,@Pattern(regexp = "^\\S{5,16}$") String phone
            ,@Pattern(regexp = "^\\S{5,16}$") String password){
        // 校验参数
        if (phone == null || password == null) {
            return Result.error("用户名或密码不能为空");
        }
        TokenVO tokenVO = userService.login(phone, password, ip, token);
        System.out.println(tokenVO);
        return Result.success(tokenVO);
    }

    /**
     * 用户登录
     * @param token
     * @param ip
     * @param phone
     * @param password
     * @return
     */
    @PostMapping("/login")
    @RequestLogin
    public Result<TokenVO> login(
                                @RequestHeader(value = CommonConstants.TOKEN_NAME, required = false) String token,
                                @RequestHeader(value = CommonConstants.REAL_IP, required = false) String ip
                                ,@Pattern(regexp = "^\\S{5,16}$") String phone
                                ,@Pattern(regexp = "^\\S{5,16}$") String password){
        // 校验参数
        if (phone == null || password == null) {
            return Result.error("用户名或密码不能为空");
        }
        TokenVO tokenVO = userService.login(phone, password, ip, token);
        System.out.println(tokenVO);
        return Result.success(tokenVO);
    }

    /**
     * 用户退出登录
     * @param token
     * @return
     */
    @PostMapping("/logout")
    public Result logout(@RequestHeader(value = CommonConstants.TOKEN_NAME, required = false) String token){
        userService.logout(token);
        return Result.success("退出登录成功");
    }



}
